package ch.zhaw.cctd.domain.wave;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.zhaw.cctd.domain.prototype.WavePrototype;
import ch.zhaw.cctd.domain.prototype.WavePrototype.WaveCreepPrototype;

/**
 * Beinhaltet den aktuellen zustand einer Wave. Wird vom GameThread benutzt um die Creeps zu spawnen.
 * 
 * @author Rolf
 */
public class Wave {

	/**
	 * Logger
	 */
	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(Wave.class);


	// Variables (Reflected from XML)
	private int spawnTime;
	private int spawnLength;
	private String name;
	private SpawnMode spawnMode;
	private List<Creeps> creepSets;
	private boolean finished;
	private int totalCreeps;
	private int currPos;

	

	/**
	 * Default Constructor
	 * 
	 * @param spawnTime
	 * @param spawnLength
	 * @param name
	 * @param spawnMode
	 * @param creepSets
	 */
	public Wave(WavePrototype prototype) {
		//Set the Variables from the Prototype
		setSpawnTime(prototype.getSpawntime());
		setSpawnLength(prototype.getSpawnlength());
		setName(prototype.getName());
		setSpawnMode(prototype.getSpawnmode());
		setCreepSets(prototype.getCreeps());
		setFinished(false);
		setCurrPos(0);
		
		//logger.trace("SpawnTime: {}, SpawnLength: {}, Name: {}, SpawnMode: {}, CreepSets: {}", spawnTime, spawnLength, name, spawnMode, creepSets.get(0).amount);
	}

	// Setters
	private void setSpawnTime(int spawnTime) {
		this.spawnTime = spawnTime;
	}

	private void setSpawnLength(int spawnLength) {
		this.spawnLength = spawnLength;
	}

	private void setName(String name) {
		this.name = name;
	}

	private void setSpawnMode(String spawnMode) {
		setSpawnMode(SpawnMode.valueOf(spawnMode.toUpperCase()));
	}

	private void setSpawnMode(SpawnMode spawnMode) {
		this.spawnMode = spawnMode;
	}

	private void setCreepSets(List<WaveCreepPrototype> creepSets) {
		this.creepSets = new ArrayList<Creeps>();
		this.totalCreeps = 0;
		for(WaveCreepPrototype wcp : creepSets) {
			this.creepSets.add(new Creeps(wcp.amount, wcp.id));
			totalCreeps+=wcp.amount;
		}
	}
	
	public void setFinished(boolean finished) {
		this.finished = finished;
	}
	
	public void setCurrPos(int currPos) {
		this.currPos = currPos;
	}
	

	// Getters
	public int getSpawnTime() {
		return spawnTime;
	}

	public int getSpawnLength() {
		return spawnLength;
	}

	public String getName() {
		return name;
	}

	public SpawnMode getSpawnMode() {
		return spawnMode;
	}
	
	public int getTotalCreeps() {
		return totalCreeps;
	}

	public List<Creeps> getCreepSets() {
		return creepSets;
	}
	
	public boolean isFinished() {
		return finished;
	}

	public int getCurrPos() {
		return currPos;
	}

	/**
	 * Used to build a Set of amount + CreepID
	 * 
	 * @author Rolf
	 * 
	 */
	public static class Creeps {
		private int amount;
		private String creepid;

		public Creeps(int a, String c) {
			this.amount = a;
			this.creepid = c;
		}

		public void lowerAmount() {
			amount--;
		}

		public int getAmount() {
			return amount;
		}

		public String getCreepid() {
			return creepid;
		}

		public String toString() {
			return "Amount: " + amount + " CreepId: " + creepid;
		}
	}



}
